<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accounts extends Admin_Controller {

	/** Begin: Config **/
	protected $table = ADMIN_USER_TABLE; // Also configed in model
	
	// Config List view
	protected $list_view_order_name = 'username';
	protected $list_view_order_direction = 'ASC';
	protected $list_view_searched_fields = array(
			'username'			=>	'Username',
			'dob'				=> 	'Ngày sinh'
		);
		
	protected $fields =  array(
			'username'		=>	array(
									'text'		=>	'Username',
									'type'		=>	'text',
									'list_view'=>array(
										'is_show_link'	=>	true
									),
									'form_view'=>array(
										'type'	=>	'text',
										'func'	=>	''
									)
								),
			'password'		=>	array(
									'text'	=>	'Password',
									'type'	=>	'password',
									'form_view'=>array(
										'type'	=>	'password',
										'func'	=>	'password'
									)
								),
			'group_id'		=>	array(
									'text'	=>	'Group',
									'type'	=>	'select',
									'select_config' => array(
										'referred_table_name' 	=> ADMIN_GROUP_TABLE,
										'referred_field_name' 	=> 'name',
										'select_default_text' 	=> 'Select group'
									),
									'form_view' => array(
									)
								),
			'dob'			=>	array(
									'text'		=>	'Ngày giờ sinh',
									'type'		=>	'text',
									'datetime_type' => 'datetime',
									'list_view'	=>array(
										'is_show_link'	=>	true
									),
									'form_view'=>array(
										'type'	=>	'text',
										'func'	=>	'date'
									)
								),
			'dob_date'		=>	array(
									'text'		=>	'Ngày sinh',
									'type'		=>	'text',
									'datetime_type' => 'date',
									'list_view'	=>array(
										'is_show_link'	=>	true
									),
									'form_view'=>array(
										'type'	=>	'text',
										'func'	=>	'date'
									)
								),
			'dob_time'		=>	array(
									'text'		=>	'Giờ sinh',
									'type'		=>	'text',
									'datetime_type' => 'time',
									'list_view'	=>array(
										'is_show_link'	=>	true
									),
									'form_view'=>array(
										'type'	=>	'text',
										'func'	=>	'date'
									)
								),
			);
	/** End: Config **/

	
	public function admincp_reset_permission(){pr('admincp_reset_permission',1);
		$data = array(
			'custom_permission'=>0,
			'permission'=>substr($this->input->post('permDefault'),6)
		);
		$this->db->where('id',$this->input->post('id'));
		if($this->db->update(ADMIN_USER_TABLE,$data)){
			print 'success';
			exit;
		}else{
			print 'error';
			exit;
		}
	}
	
	public function admincp_ajaxPerm($group_id){
		$this->load->model('manager_modules/manager_modules_model');
		$this->load->model('account_groups/account_groups_model');
		$list_modules = $this->manager_modules_model->list_module();
		$default_perm = '';
		if($this->input->post('perm')){
			$result = json_decode('{"permission":"'.$this->input->post('perm').'"}');
			$default_perm = $this->account_groups_model->getDetailManagement($group_id);
		}else{
			$result = $this->account_groups_model->getDetailManagement($group_id);
		}
		$data = array(
			'list_modules'=>$list_modules,
			'result'=>$result,
			'default_perm'=>$default_perm
		);
		$this->load->view('BACKEND/ajax_permission',$data);
	}
	
	
	public function admincp_saveAction(){
		//Permission list module
		$this->load->model('manager_modules/manager_modules_model');
		$list_modules = $this->manager_modules_model->list_module();
		$perm = '';
		foreach($list_modules as $k=>$v){
			if($k==0){
				$perm .= $v->id.'|';
			}else{
				$perm .= ','.$v->id.'|';
			}
			if($this->input->post('read'.$v->id.'Admincp')=='on'){
				$perm .= 'r';
			}else{
				$perm .= '-';
			}
			if($this->input->post('write'.$v->id.'Admincp')=='on'){
				$perm .= 'w';
			}else{
				$perm .= '-';
			}
			if($this->input->post('delete'.$v->id.'Admincp')=='on'){
				$perm .= 'd';
			}else{
				$perm .= '-';
			}
		}
		
		$arr_perm_group = explode(',',substr($this->input->post('perm_group'),6));
		foreach($arr_perm_group as $v){
			$custom_perm = 0;
			if(strpos($perm,$v)===false){
				$custom_perm = 1;
				break;
			}
		}
		
		$data['custom'] = array(
			'permission'				=>	$perm,
			'custom_permission'	=>	$custom_perm
			);
		$data['FILES'] = parent::admincp_uploadFiles();
		return $data;
	}

	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	/*------------------------------------ End FRONTEND --------------------------------*/
}